import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import UpVip from '../views/UpVip.vue'
import NoteLibrary from '../views/NoteLibrary.vue'
import Highlights from '../views/Highlights.vue'
import User from '../views/User.vue'
import NoteEditor from '../views/NoteEditor.vue'
import Error404 from '../views/Error.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    component: Home
  },
  {
    path: '/upvip',
    component: UpVip
  },
  {
    path: '/notelibrary',
    component: NoteLibrary
  },
  {
    path: '/highlights',
    component: Highlights
  },
  {
    path: '/help',
    component: Highlights
  },
  {
    path: '/user',
    component: User
  },
  {
    path: '/noteeditor',
    component: NoteEditor
  },
  {
    name:"noteeditorid",
    path: '/noteeditor/:id',
    component: NoteEditor
  },
  {
    path: '/404',
    component: Error404
  }
]

const router = new VueRouter({
  routes,
  // mode:'hash'
  mode: 'history'
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  let id = to.params.id;
  if (to.path === "/home") {
    return next();
    // return next("/error404");
  } else if (to.path === "/user") {// 如果要访问的页面为个人中心页
    // 获取token
    const tokenStr = window.sessionStorage.getItem("token");
    // 如果token为空
    if (!tokenStr) {
      return next("/home");
    }
    next();
  } else if(to.path === "/404"){
    // return next("/error404");
    return next();
  }else if(to.path === "/noteeditor"){
    return next();
  }
  else if(to.path === "/noteeditor/"+id){
    return next();
  }
  // else if(to.path.slice(0,11) === "/noteeditor"){
  //   return next("/noteeditor");
  // }
  else{
    return next("/404");
  }
})

export default router
